/*!
 Temelia - Scapegoat tree samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/scapegoat_tree.h>

static void sample1()
{
	int **x, i, N = 100;
	scapegoat_tree_t scapegoat = NULL;

	PRINT("\n\n Sample 1 started \n\n");

	x = int_array_new(N, N);
	scapegoat = scapegoat_tree_new(0.7);

	for (i = 0; i < N; i++)
		scapegoat_tree_insert(scapegoat, x[i], int_compare, NULL);

	PRINT("Empty : %d, Leaf : %d\n", scapegoat_tree_is_empty(scapegoat),
			scapegoat_tree_is_leaf(scapegoat_tree_get_root(scapegoat)));

	PRINT("Size = %d", scapegoat_tree_get_size(scapegoat));
	PRINT("Root %p %p\n", scapegoat_tree_get_root(scapegoat),
			scapegoat_tree_get_node_root(scapegoat_tree_get_root(scapegoat)));
	PRINT("Root key %p\n", scapegoat_tree_get_key(scapegoat_tree_get_root(
							scapegoat)));
	PRINT("Root's : parent %p, left child %p, right child %p\n",
			scapegoat_tree_get_parent(scapegoat_tree_get_root(scapegoat)),
			scapegoat_tree_get_left_child(scapegoat_tree_get_root(scapegoat)),
			scapegoat_tree_get_right_child(scapegoat_tree_get_root(scapegoat)));

	PRINT("Root's height : %d, depth %d\n", scapegoat_tree_get_node_height(
					scapegoat_tree_get_root(scapegoat)), scapegoat_tree_get_depth(
					scapegoat_tree_get_root(scapegoat)));
	PRINT("Min %p, Max %p\n", scapegoat_tree_get_min(scapegoat),
			scapegoat_tree_get_max);

	scapegoat_tree_show_indented(scapegoat, int_level_handler, NULL);

	for (i = 0; i < N; i++)
		if (scapegoat_tree_search(scapegoat, x[i], int_compare, NULL) == NULL)
		{
			PRINT("Scapegoat error; check this line : %d\n", __LINE__);
			exit(1);
		}

	for (i = 0; i < N; i++)
		scapegoat_tree_remove(scapegoat, x[i], int_compare, NULL);

	scapegoat_tree_delete(scapegoat);

	int_array_delete(x, N);

	PRINT("\n\n Sample 1 ended \n\n");
}

static void sample2()
{
	int **x, i, N = 100;
	scapegoat_tree_t scapegoat = NULL;

	PRINT("\n\n Sample 2 started \n\n");

	x = int_array_new(N, N);
	scapegoat = scapegoat_tree_new(0.55);

	for (i = 0; i < N; i++)
		scapegoat_tree_insert(scapegoat, x[i], int_compare, NULL);

	PRINT("\n\n Indented print :\n\n");
	scapegoat_tree_show_indented(scapegoat, int_level_handler, NULL);

	PRINT("\n\n Preorder :\n\n");
	scapegoat_tree_preorder(scapegoat, int_handler, NULL);

	PRINT("\n\n Inorder :\n\n");
	scapegoat_tree_inorder(scapegoat, int_handler, NULL);

	PRINT("\n\n Reverse Inorder\n\n");
	scapegoat_tree_reverse_inorder(scapegoat, int_handler, NULL);

	PRINT("\n\n Postorder\n\n");
	scapegoat_tree_postorder(scapegoat, int_handler, NULL);

	PRINT("\n\n Level order\n\n");
	scapegoat_tree_level_order(scapegoat, int_handler, NULL);

	scapegoat_tree_delete(scapegoat);

	int_array_delete(x, N);

	PRINT("\n\n Sample 2 ended \n\n");
}

static void sample3()
{
	int **x, N = 100, i;
	scapegoat_tree_t scape1, scape2, scape3;

	PRINT("\n\n Sample 3 started \n\n");

	x = int_array_new(N, N);
	scape1 = scapegoat_tree_new(0.6);
	scape2 = scapegoat_tree_new(0.6);
	scape3 = scapegoat_tree_new(0.99);

	for (i = 0; i < N; i++)
	{
		scapegoat_tree_insert(scape1, x[i], int_compare, NULL);
		scapegoat_tree_insert(scape2, x[i], int_compare, NULL);
		scapegoat_tree_insert(scape3, x[i], int_compare, NULL);
	}

	PRINT("Compare %d\n", scapegoat_tree_compare_trees(scape1, scape2,
					int_compare, NULL));

	PRINT("\n\n Indented print for alpha = %lf :\n\n",
			scapegoat_tree_get_alpha(scape1));
	scapegoat_tree_show_indented(scape1, int_level_handler, NULL);
	PRINT("======> Tree's height %d", scapegoat_tree_get_height(scape1));

	PRINT("\n\n Indented print for alpha = %lf :\n\n",
			scapegoat_tree_get_alpha(scape3));
	scapegoat_tree_show_indented(scape3, int_level_handler, NULL);
	PRINT("======> Tree's height %d", scapegoat_tree_get_height(scape3));

	scapegoat_tree_delete(scape1);
	scapegoat_tree_delete(scape2);
	scapegoat_tree_delete(scape3);

	int_array_delete(x, N);

	PRINT("\n\n Sample 3 ended \n\n");
}

void run_scapegoat_tree_samples()
{
	sample1();
	sample2();
	sample3();
}
